/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uloha4.model;

import uloha4.entities.Component;
import uloha4.entities.ComponentList;
import uloha4.entities.Node;
import javax.swing.table.AbstractTableModel;
import uloha4.view.MagicCanvas;

/**
 *
 * @author Tomique
 */
public class ComponentTableModel extends AbstractTableModel {

    // nadpisy jednotlivych sloupcu
    private String[] colNames = {"", "Component", "# Nodes", "# Edges"};
    // data, ktera budou vyobrazena
    private ComponentList components;
    // 
    private MagicCanvas canvas;

    public ComponentTableModel() {
        this.components = ComponentList.getInstance();
        this.canvas = MagicCanvas.getInstance();
    }

    @Override
    public String getColumnName(int column) {
        return colNames[column];
    }

    @Override
    public int getRowCount() {
        return components.size();
    }

    @Override
    public int getColumnCount() {
        return colNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Component<Node> comp = components.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return comp.isVisible();
            case 1:
                return comp.getRepresentative();
            case 2:
                return comp.getNodes();
            case 3:
                return comp.getEdges();
        }
        throw new RuntimeException("Impossible switch state");
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return column == 0 ? true : false;
    }
    
    @Override
    public void setValueAt(Object value, int row, int column) {
        if(column != 0) {
            return;
        }
        
        Component<Node> comp = components.get(row);
        
        if(value.toString().equals("true")) {
            comp.setVisible(true);
        } else {
            comp.setVisible(false);
        }
        canvas.repaint();
    }

    /**
     * Prepsanim teto metody dojde ke zobrazeni checkboxu namisto slovni hodnoty
     *
     * @param index
     * @return
     */
    @Override
    public Class getColumnClass(int index) {
        return this.getValueAt(0, index).getClass();
    }
    
    
    
    private void testData() {
        Node node1 = new Node("Pepa", 1, 10, 10);
        Node node2 = new Node("Karel", 2, 0, 0);

        node1.addDirectedNeighbour(null);
        node1.addDirectedNeighbour(null);
        node1.addDirectedNeighbour(null);

        node2.addDirectedNeighbour(null);
        node2.addDirectedNeighbour(null);
        node2.addDirectedNeighbour(null);
        node2.addDirectedNeighbour(null);

        Component<Node> component1 = new Component<>();
        component1.add(node1);
        component1.add(node2);

        Component<Node> component2 = new Component<>();
        component2.add(node2);

        components.add(component1);
        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
//        components.add(component1);
//        components.add(component2);
    }
}
